package utils

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"net/http"
)

type OutPut struct {
	Code    int64       `json:"code"`
	Data    interface{} `json:"data,omitempty"`
	Message string      `json:"message"`
}

// OutSuccess 业务正常回复
func OutSuccess(ctx *gin.Context, data interface{}) {
	//设置新的token
	if token, ok := ctx.Get("NewAuthorization"); ok {
		ctx.Header("newAuthorization", cast.ToString(token))
	}

	res := &OutPut{
		Code:    200,
		Data:    data,
		Message: "成功",
	}
	ctx.JSON(http.StatusOK, res)
	ctx.Abort()
}

// OutWrong 业务异常回复
func OutWrong(ctx *gin.Context, code int64, message string, data interface{}) {
	res := &OutPut{
		Code:    code,
		Data:    data,
		Message: message,
	}
	ctx.JSON(http.StatusOK, res)
	ctx.Abort()
}

// LoginOut 业务异常回复
func LoginOut(ctx *gin.Context) {
	ctx.JSON(http.StatusUnauthorized, nil)
}

// Forbidden 拒绝
func Forbidden(ctx *gin.Context,name string) {
	ctx.JSON(http.StatusForbidden, name)
}
